package com.szkjt.action;

import java.io.IOException;
import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import net.sf.json.JSONObject;

import com.szkjt.model.Finance;
import com.szkjt.service.BaseService;
import com.szkjt.service.FinanceService;
import com.szkjt.service.StaffService;

@Component @Scope("prototype")
public class FinanceAction extends BaseAction<Finance> {

	private FinanceService financeService;
	private StaffService staffService;
	
	private RuntimeService runtimeService;
	private TaskService taskService;
	
	@Override
	public Finance saveOrUpdate() throws IOException {
		Finance finance = super.saveOrUpdate();
		
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("finance");
		Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).taskDefinitionKey("submitReport").singleResult();
		
		taskService.addCandidateUser(task.getId(), finance.getStaff().getId() + "");
		
		finance.setProcessId(processInstance.getId());
		financeService.saveOrUpdate(finance);
		
		return null;
	}
	
	public void getFinanceByStaff() throws IOException {
		Task task = taskService.createTaskQuery().taskCandidateUser("202").list().get(0);
		Finance finance = financeService.getByProcessId(task.getProcessInstanceId());
		JSONObject object = getJSONObject(finance);
		
		JSONObject jo = new JSONObject();
		jo.put("success", true);
		jo.put(DATA, object);
		responseData(jo);
	}
	
	@Override
	protected BaseService<Finance> getBaseService() {
		return financeService;
	}

	@Override
	protected JSONObject getJSONObject(Finance finance) {
		JSONObject jo = new JSONObject();
		jo.put("id", finance.getId());
		jo.put("companyName", finance.getCompanyName());
		jo.put("contactName", finance.getContactName());
		jo.put("contactPhone", finance.getContactPhone());
		jo.put("expectAmount", finance.getExpectAmount());
		
		return jo;
	}

	@Override
	protected Finance getModel() {
		Finance finance = financeService.getById(getParamValue("id"));
		finance = (finance != null) ? finance : new Finance();
		finance.setCompanyName(getParamValue("companyName"));
		finance.setContactName(getParamValue("contactName"));
		finance.setContactPhone(getParamValue("contactPhone"));
		finance.setExpectAmount(getParamValue("expectAmount"));
		finance.setStaff(staffService.getById(getParamValue("staffId")));
		
		return finance;
	}

	@Override
	protected boolean validateModel() throws IOException {
		return true;
	}

	//---��ȡ�����÷���---
	public FinanceService getFinanceService() {
		return financeService;
	}

	public void setFinanceService(FinanceService financeService) {
		this.financeService = financeService;
	}

	public StaffService getStaffService() {
		return staffService;
	}

	public void setStaffService(StaffService staffService) {
		this.staffService = staffService;
	}

	public RuntimeService getRuntimeService() {
		return runtimeService;
	}

	public void setRuntimeService(RuntimeService runtimeService) {
		this.runtimeService = runtimeService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}
	
}
